$(function () {

  let layer = layui.layer;



//获取用户信息
option()
function option() {
  

  $.ajax({
    url: `admin/swipers`,
    success: function (res) {
      console.log(res);
      
      let Str = template("addtr", res)
      $("#tb").html(Str)

    }

  })

}


//修改轮播图状态

$("#tb").on("click", ".mystate", function (a) {
  a.preventDefault()
  let id = $(this).attr("myid")//获取id
  let num = $(this).attr("num")//获取button里的num 
  $.ajax({
  type: `PUT`,
  url: `admin/swipers/${id}`,
  data: {
    status:num//发送请求，修改status内容
  },
  success:function (res) {

if (res.status !== 0) {
  return layer.msg(res.message);
}
layer.msg(res.message);
option()
  }

  })
  
})




//批量上传轮播图

$(".addimg").on("click",function () {
  $("[type=file]").click()
})


 $("body").on("change", "[type=file]", function () {
   let ds = this.files
   let fd = new FormData()
   $.each(ds, function (index, item) {
     fd.append('swipers', item)
   })
   $.ajax({
     type:`post`,
     url: `admin/swipers`,
     processData: false,
     contentType: false,
     data:fd,
     success:function (res) {
     if (res.status !== 0) {
       return layer.msg(res.message);     
     }
     layer.msg(res.message);
     option()
       
     }
   })
   
 })






//删除图片


$("#tb").on("click",".del",function () {
  let id = $(this).attr("myid")



   layer.confirm('是否删除图片？?', {
     icon: 3,
     title: '删除'
   }, function (index) {
if (index !== 0) {
   $.ajax({
     type: `DELETE`,
     url: `admin/swipers/${id}`,
     success: function (res) {

       if (res.status !== 0) {
         return layer.msg(res.message);
       }
       layer.msg(res.message);
       option()
     }
   })
}
     layer.close(index);
     
   });

  
})




})